Spree::Sample.load_sample("taxonomies")
Spree::Sample.load_sample("products")

themes = Spree::Taxonomy.find_by_name!("场景搭配")
brands = Spree::Taxonomy.find_by_name!("设计品牌")
types = Spree::Taxonomy.find_by_name!("单品分类")

products = {
    :ror_tote => "Ruby on Rails Tote",
}

def image(name, type="jpg")
  images_path = Pathname.new(File.dirname(__FILE__)) + "images"
  path = images_path + "#{name}.#{type}"
  return false if !File.exist?(path)
  File.open(path)
end


products.each do |key, name|
  products[key] = Spree::Product.find_by_name!(name)
end

taxons = [
    {
        :name => "场景搭配",
        :taxonomy => themes,
        :position => 0
    },
    {
        :name => "商务职业",
        :taxonomy => themes,
        :parent => "场景搭配",
        :position => 1,
        :icon => image('theme_profeesional'),
        :products => [
            products[:ror_tote],
        ]
    },
    {
        :name => "逛街",
        :taxonomy => themes,
        :parent => "场景搭配",
        :position => 2,
        :icon => image('theme_shopping'),
        :products => [
            products[:ror_tote],
        ]
    },
    {
        :name => "商务休闲",
        :taxonomy => themes,
        :parent => "场景搭配",
        :position => 3,
        :icon => image('theme_business'),
        :products => [
            products[:ror_tote],
        ]
    },
    {
        :name => "约会",
        :taxonomy => themes,
        :parent => "场景搭配",
        :position => 4,
        :icon => image('theme_dating'),
        :products => [
            products[:ror_tote],
        ]
    },

    {
        :name => "设计品牌",
        :taxonomy => brands,
        :position => 1
    },

    {
        :name => "redioactive",
        :taxonomy => brands,
        :parent => "设计品牌",
        :icon => image('fabulisher-logo', 'png'),
        :products => [
            products[:ror_tote]
        ]
    },

    {
        name: "单品分类",
        taxonomy: types,
        position: 2
    },
    {
        :name => "上衣",
        :taxonomy => types,
        :parent => "单品分类",
        :position => 1,
        :icon => image('type_coat'),
        :products => [
            products[:ror_tote],
        ]
    },
    {
        :name => "大衣",
        :taxonomy => types,
        :parent => "单品分类",
        :position => 2,
        :icon => image('type_overcoat'),
        :products => [
            products[:ror_tote],
        ]
    },
    {
        :name => "半身裙",
        :taxonomy => types,
        :parent => "单品分类",
        :position => 3,
        :icon => image('type_skirt'),
        :products => [
            products[:ror_tote],
        ]
    },
    {
        :name => "连衣裙",
        :taxonomy => types,
        :parent => "单品分类",
        :position => 4,
        :icon => image('type_dress'),
        :products => [
            products[:ror_tote],
        ]
    },
    {
        :name => "夹克",
        :taxonomy => types,
        :parent => "单品分类",
        :position => 5,
        :icon => image('type_jacket'),
        :products => [
            products[:ror_tote],
        ]
    },
]

taxons.each do |taxon_attrs|
  if taxon_attrs[:parent]
    taxon_attrs[:parent] = Spree::Taxon.where(name: taxon_attrs[:parent]).first
    Spree::Taxon.create!(taxon_attrs)
  end
end
